package BandB.Tool.GPhoneManager.FileExplorer;

//import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import BandB.Tool.GPhoneManager.R;
import BandB.Tool.GPhoneManager.FileExplorer.Util.IconFactory;
import BandB.Tool.GPhoneManager.FileExplorer.Util.Utilities;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class FileAdapter extends ArrayAdapter<FileItem> {

	private final LayoutInflater mInflater;
	private static Context mContext;
	boolean isRootDirectory;
	private static FileComparator fileComparator = new FileComparator();

	// private static File mCurrentDirectory;

	public FileAdapter(Context context, List<FileItem> files, int mode) {
		super(context, 0, files);
		mContext = context;
		mInflater = LayoutInflater.from(context);
		currentMode = mode;
		sortFileList();
		// initCheckCache();
	}

	@Override
	public void add(FileItem object) {
		// initCheckCache();
		super.add(object);
	}

	//
	// public FileAdapter() {
	// super(mContext, currentMode);
	// mInflater = LayoutInflater.from(mContext);
	// }

	@SuppressWarnings("unchecked")
	private void sortFileList() {
		sort(fileComparator);
	}

	// private void initCheckCache() {
	// checkCache.clear();
	// for (int i = 0; i < getCount(); i++) {
	// checkCache.put(i, false);
	// Log.i(TAG, i + " cache is " + checkCache.get(i));
	// }
	//
	// }

	public void setCheckAt(int position) {
		if (currentMode == FileExplorer.MODE_MULTIPLE) {
			FileItem fileItem = getItem(position);
			fileItem.isChecked = !fileItem.isChecked;
			notifyDataSetChanged();
		}
	}

	public List<FileItem> getCheckFile() {
		List<FileItem> checkFiles = new ArrayList<FileItem>();
		int count = getCount();
		for (int i = 0; i < count; i++) {
			if (null != getItem(i)) {
				if (getItem(i).isChecked) {
					Log.i(TAG, "get check at " + i);
					checkFiles.add(getItem(i));
				}
			}
		}
		return checkFiles;
	}

	@SuppressWarnings("unchecked")
	public static class FileComparator implements Comparator {

		public int compare(Object op1, Object op2) {
			if (null == op1 || null == op2) {
				return 0;
			}
			FileItem f1 = (FileItem) op1;
			FileItem f2 = (FileItem) op2;

			if (f1.file.isDirectory() && f2.file.isFile()) {
				return -1;
			} else if (f1.file.isFile() && f2.file.isDirectory()) {
				return 1;
			} else {
				return f1.file.getName().compareTo(f2.file.getName());
			}
		}
	}

	private static int currentMode;

	// set the show mode on single and multiple
	public void setCurrentMode(int mode) {
		currentMode = mode;
		// if(mode == FileExplorer.MODE_MULTIPLE){
		// initCheckCache();
		// }
		notifyDataSetChanged();
	}

	private void setCheckBox(int position, View convertView) {
		final CheckBox checkBox = (CheckBox) convertView
				.findViewById(R.id.file_check);
		checkBox.setTag(position);
		checkBox.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int position = (Integer) v.getTag();
				setCheckAt(position);
			}

		});
		// if current mode is multiple choice mode , we set the checkbox status
		if (currentMode == FileExplorer.MODE_MULTIPLE) {
			checkBox.setVisibility(View.VISIBLE);
			checkBox.setChecked(getItem(position).isChecked);
		} else if (currentMode == FileExplorer.MODE_SINGLE) {
			checkBox.setVisibility(View.GONE);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.file_list_item, parent,
					false);
		}
		final TextView fileNameView = (TextView) convertView
				.findViewById(R.id.file_name);
		final ImageView fileIconView = (ImageView) convertView
				.findViewById(R.id.file_icon);

		Drawable icon;
		String name;
		final FileItem fileItem = getItem(position);
		if (null != fileItem) {
			icon = IconFactory.getInstance(mContext).getFileIcon(fileItem.file);
			if (fileItem.file.isDirectory()) {
				name = fileItem.file.getName();
			} else {
				name = fileItem.file.getName() + "("
						+ Utilities.formateSize(fileItem.file.length()) + ")";
			}

			convertView.setTag(fileItem);
			fileNameView.setText(name);
			fileIconView.setBackgroundDrawable(icon);
			setCheckBox(position, convertView);
		}

		return convertView;
	}

	private String TAG = FileAdapter.class.getSimpleName();

}
